package alg20250227;

public class Text {
    public int rob(int[] nums) {
        int n = nums.length;
        return Math.max(nums[0] + maxPrice(nums, 2, n - 2), maxPrice(nums, 1, n - 1));
    }

    public int maxPrice(int[] nums, int left, int right){
        if(left > right) return 0;
        //1 创建表
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];   //方便下标 映射
        //2 初始化
        f[left] = nums[left];
        //3 填表
        for (int i = left + 1; i <= right; i++) {
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(f[i - 1], g[i - 1]);
        }
        //4 返回值
        return Math.max(f[right], g[right]);
    }
}
